<?php
namespace Home\Controller;

use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/13
 * Time: 13:40
 */
class UserFeedbackController extends CommnController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 文字反馈列表   type==1
     */
    public function user_list_feedback()
    {

        $feedbackModel = M('user_feedback', '', 'KEFU');
        $search = I('get.feedback_word', '');
        $status=I('get.status','0');
        $where = [];
        $where['type'] = array('eq', '1');
        $tol = $feedbackModel->where($where)->count();
        $row = '13';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();

        if (!empty($search)) {
            $where = [];
            $no = strstr('未回复', $search);
            $yes = strstr('已回复', $search);
            $finish = strstr('已完成', $search);
            if ($no != '') {
                $where['b.status'] = array('eq', '1');
            }
            if (!empty($yes)) {
                $where['b.status'] = array('eq', '2');
            }
            if (!empty($finish) || $finish != '') {
                $where['b.status'] = array('eq', '3');
            }

            if (empty($yes) && empty($finish) && empty($no)) {
                $where['b.content|b.mobile|a.truename'] = array('like', '%' . $search . '%');
            }
            if($status!='0'){
                $where['status'] = array('eq',$status);
            }
            $where['type'] = array('eq', '1');
            $tol = $feedbackModel->where($where)->count();
            $page = new PageController($tol, $row);
            $field = 'b.content,b.id,b.mobile,b.ctime,b.status,b.type,b.upd_time,b.vid';
            $data = $feedbackModel->join('b join dxd_user.user a on b.mobile=a.mobile')->LIMIT($page->listfirst, $page->listRows)->where($where)->field($field)->order('b.id desc')->select();
        } else {
            if($status!='0'){
                $where['status'] = array('eq',$status);
            }
            $data = $feedbackModel->where($where)->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        }

        $user = M('user', '', 'USER');

        foreach ($data as $k => $v) {
            $where = [];
            $where['vid'] = array('eq', $v['vid']);
            $name = $user->where($where)->field('mobile,truename')->find();

            if (!empty($name['truename'])) {
                $data[$k]['username'] = $name['truename'];
            } else {
                $data[$k]['username'] = $name['mobile'];
            }

            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $data[$k]['back_time'] = date('Y-m-d H:i', $v['upd_time']);
        }

        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));

        $this->display();
    }

    /**
     * 与 电话反馈列表
     */
    public function user_list_phone()
    {
        $where = [];
        $where['type'] = array('eq', '2');
        $feedbackModel = M('user_feedback', '', 'KEFU');
        $tol = $feedbackModel->where($where)->count();
        $row = '12';
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();


        $search = I('get.feedback_phone');

        if (!empty($search)) {

            if (empty($yes) && empty($finish) && empty($no)) {
                $where['b.content|b.mobile|a.truename'] = array('like', '%' . $search . '%');
            }
            $tol = $feedbackModel->where($where)->count();
            $page = new PageController($tol, $row);
            $field = 'b.content,b.id,b.mobile,b.ctime,b.status,b.type,a.truename';
            $data = $feedbackModel->join('b join dxd_user.user a on b.mobile=a.mobile')->LIMIT($page->listfirst, $page->listRows)->where($where)->order('b.id desc')->select();

        } else {
            $data = $feedbackModel->where($where)->order('id desc')->LIMIT($page->listfirst, $page->listRows)->select();
        }


        foreach ($data as $k => $v) {

            $name = M('user', '', 'USER')->where(['vid' => $v['vid']])->field('truename,mobile')->find();
            if (empty($name['truename'])) {
                $data[$k]['username'] = $name['mobile'];
            } else {
                $data[$k]['username'] = $name['truename'];
            }

            //受理客服
            $admin_id = M('user_message', '', 'KEFU')->where(['uf_id' => $v['id']])->getfield('admin_name');
//            $admin_name = M('admin', '', 'KEFU')->where(['id' => $admin_id['admin_id']])->getfield('name');
            $data[$k]['admin_name'] = $admin_id;

            //回复状态
            if ($v['status'] == '1') {
                $data[$k]['status'] = '未回复';
            } else if ($v['status'] == '2') {
                $data[$k]['status'] = '已回复';
            } else if (($v['status'] == '3')) {
                $data[$k]['status'] = '完成';
            }

            //回复方式
            if ($v['type'] == '1') {
                $data[$k]['type'] = '短信反馈';
            } else if ($v['type'] == '2') {
                $data[$k]['type'] = '电话反馈';
            }
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            $data[$k]['back_time'] = date('Y-m-d H:i', $v['back_time']);
        }


        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'tol' => $tol
        ));
        $this->display();
    }


    //文字反馈详情
    public function feedback_detail($id)
    {
        $feedback_info = M('user_feedback', '', 'KEFU')->where(['id' => $id])->find();
        $name = M('user', '', 'USER')->where(['vid' => $feedback_info['vid']])->Field('truename,id')->find();
        if (empty($name['truename'])) {
            $name['truename'] = $feedback_info['mobile'];
        }

        $detail_list = M('user_message', '', 'KEFU')->where(['uf_id' => $id])->select();

        foreach ($detail_list as $k=>$v){
//            $record = M('admin', '', 'KEFU')->where(['id'=>$v['admin_id']])->getfield('name');
//            $detail_list[$k]['admin']=$record;
            $detail_list[$k]['ctime']= date('Y-m-d H:i', $v['ctime']);

        }

        if (!empty($feedback_info['img'])) {
            $imgs = explode(',', $feedback_info['img']);
        } else {
            $imgs = '';
        }

        $this->assign(array(
            'name' => $name['truename'],
            'content' => $feedback_info['content'],
            'imgs1' => $imgs,
            'ctime' => date('Y-m-d H:i', $feedback_info['ctime']),
            'id' => $id,
            'user_id'=>$name['id'],
            'list' => $detail_list
        ));
        $this->display();
    }

    //电话反馈详情
    public function user_list_phone_details($id)
    {
        $feedback_info = M('user_feedback', '', 'KEFU')->where(['id' => $id])->find();
        $detail_list = M('user_message', '', 'KEFU')->where(['uf_id' => $id])->select();

        foreach ($detail_list as $k=>$v){
            $detail_list[$k]['ctime']=date('Y-m-d H:i:s',$v['ctime']);
        }

        $name = M('user', '', 'USER')->where(['vid' => $feedback_info['vid']])->Field('truename,mobile')->find();
        if (empty($name['truename']) || $name['truename'] = '') {
            $user_name = $name['mobile'];
        } else {
            $user_name = $name['truename'];
        }
        //受理客服
//        $admin_name = M('admin', '', 'KEFU')->where(['id' => $detail_list['admin_id']])->getfield('name');

        $this->assign(array(
            'user_name' => $user_name,
            'content' => $feedback_info['content'],
            'ctime' => date('Y-m-d H:i', $feedback_info['ctime']),
            'list' => $detail_list
        ));

        $this->display();
    }

    //回复反馈
    public function back_userFeedback()
    {
        $content = trim(I('back_content'));
        $id = I('id');
        $name = I('name');
        $admin_name=$_SESSION['name'];
        if (!empty($content)) {
            $data=[];
            $data['status'] = '2';
            $back = M('user_feedback', '', 'KEFU')->where(['id' => $id])->save($data);


            $str = array(
                'ctime' => time(),
                'vid' => '0',
                'uf_id' => $id,
                'content' => $content,
                'img' =>I('img'),
                'admin_name' => $admin_name
            );
            $mesg = M('user_message', '', 'KEFU')->add($str);
            if ($mesg) {
                $info = M('user_feedback', '', 'KEFU')->where(['id' => $id])->find();
                M('user_feedback', '', 'KEFU')->where(['id' => $id])->setfield('rid_status',1);
                $str = array('aid' => $_SESSION['id'], 'content' => '回复第' . $id . '条' . $info['mobile'] . '的反馈成功', 'ctime' => time());
                $record = M('admin_log', '', 'KEFU')->add($str);
                if ($record) {
                    $this->ajax('1001', '回复成功');
                } else {
                    $this->ajax('1002', '回复失败1');
                }
                $this->redirect('UserFeedback/user_list_feedback');
            } else {
                $this->ajax('1002', '回复失败2');
            }
        }
    }
    //电话回复反馈
    public function feedback_phone()
    {
        $admin_name=$_SESSION['name'];
        if (empty($_POST)) {
            $this->display();
        } else {
            $phone = trim(I('phone'));
            $content = trim(I('content'));
            $back_content = trim(I('back_content'));

            if ($phone == '' || $content == '' || $back_content == '') {
                $this->ajax('1002', '请填写完整');
                return false;
            }

            $vid = M('user', '', 'USER')->where(['mobile' => $phone])->getfield('vid');
            if (empty($vid)) {
                $this->ajax('1002', '用户账户号码不对！');
                return false;
            }

            $str = array('vid' => $vid, 'content' => $content, 'ctime' => time(), 'mobile' => $phone, 'back_time' => time(), 'back_content' => $back_content, 'status' => '2', 'type' => '2');

            $insert = M('user_feedback', '', 'KEFU')->add($str);

            if ($insert) {
                $arr = array('vid' => '0', 'content' => $back_content, 'ctime' => time(), 'uf_id' => $insert, 'img' => '','admin_name' => $admin_name);
                $add = M('user_message', '', 'KEFU')->add($arr);
                if ($add) {
                    $this->ajax('1001', '添加成功');
                } else {
                    $this->ajax('1002', '添加失败');
                }
            } else {
                $this->ajax('1002', '添加失败');
            }

        }

    }

    public function ajax($code, $result, $r = '')
    {
        if (empty($r)) {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
        } else {
            $this->ajaxReturn(['code' => $code, 'result' => $result]);
            return false;
        }
    }
}